package com.tanhua.server.controller;

import cn.hutool.core.convert.Convert;
import com.tanhua.common.vo.ErrorResult;
import com.tanhua.server.service.MyCenterService;
import com.tanhua.server.vo.CountsVo;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RequestMapping("users")
@RestController
@Slf4j
public class MyCenterController {

    @Autowired
    private MyCenterService myCenterService;

    /**
     * 根据用户id查询用户信息
     *
     * @param userId 用户id，如果为空，表示查询当前登录人的信息
     * @return
     */
    @GetMapping
    public UserInfoVo queryUserInfoByUserId(@RequestParam(value = "userID", required = false) Long userId) {
        return this.myCenterService.queryUserInfoByUserId(userId);
    }

    /**
     * 是否喜欢
     *
     * @param userId
     * @return
     */
    @GetMapping("{userId}/alreadyLove")
    public Boolean isLike(@PathVariable("userId") Long userId){
        return this.myCenterService.isLike(userId);
    }

    /**
     * 互相喜欢，喜欢，粉丝 - 统计
     *
     * @return
     */
    @GetMapping("counts")
    public CountsVo queryCounts() {
        return this.myCenterService.queryCounts();
    }

    /**
     * 互相关注、我关注、粉丝、谁看过我 - 翻页列表
     *
     * @param type     1 互相关注 2 我关注 3 粉丝 4 谁看过我
     * @param page
     * @param pageSize
     * @param nickname
     * @return
     */
    @GetMapping("/friends/{type}")
    public Object queryLikeList(@PathVariable("type") String type,
                                                    @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                    @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
                                                    @RequestParam(value = "nickname", required = false) String nickname) {

            page = Math.max(1, page);
            PageResult pageResult = this.myCenterService.queryLikeList(Integer.valueOf(type), page, pageSize, nickname);
            return pageResult;
    }


//设置陌生人问题，将陌生人问题保存到数据库
    @PostMapping("/questions")
    public ErrorResult question(@RequestBody Map<String,Object> param){
        String content = Convert.toStr(param.get("content"));

        return  this.myCenterService.saveQuestions(content);
    }

    @GetMapping("/settings")
    public Object querySettings(){
       return this.myCenterService.querySettings();
    }

    @PostMapping("/notifications/setting")
    public ErrorResult setting(@RequestBody Map<String,Object> param){
       Boolean likeNotification = Convert.toBool(param.get("likeNotification"),false);
        Boolean pinglunNotification = Convert.toBool(param.get("pinglunNotification"),false);
        Boolean gonggaoNotification = Convert.toBool(param.get("gonggaoNotification"),false);

       return this.myCenterService.setting(likeNotification,pinglunNotification,gonggaoNotification);

    }

       @GetMapping("/blacklist")
        public Object blacklist(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize){
       return this.myCenterService.blacklist(page,pageSize);
        }

        @DeleteMapping("/blacklist/{uid}")
    public  Object deleteBlacklist(@PathVariable("uid")Long uid){
          return   this.myCenterService.deleteBlacklist(uid);
        }

    /**
     * 取消喜欢
     *
     * @param uid
     * @return
     */
    @DeleteMapping("like/{uid}")
    public ResponseEntity<Void> disLike(@PathVariable("uid") Long uid) {

            this.myCenterService.disLike(uid);
            return ResponseEntity.ok(null);
       // return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 关注粉丝
     *
     * @param uid
     * @return
     */
    @PostMapping("fans/{uid}")
    public ResponseEntity<Void> likeFan(@PathVariable("uid") Long uid){

            this.myCenterService.likeFan(uid);
            return ResponseEntity.ok(null);
       // return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 更新用户信息
     *
     * @param userInfoVo
     * @return
     */
    @PutMapping
    public ResponseEntity<Void> updateUserInfo(@RequestBody UserInfoVo userInfoVo){
            Boolean bool = this.myCenterService.updateUserInfo(userInfoVo);
            if(bool){
                return ResponseEntity.ok(null);
            }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

}
